﻿using BLL;
using Inventory.comm;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Inventory.sontemplate
{
    public partial class _InStockPurchaseBill : Inventory.template.BasepurchaseBillFrom
    {
        List<string> IDList;//待入库订单列表泛型集合
        InStockPurchaseBillBLL bl;//入库页面业务逻辑类
        string opera;//记录操作类型。入库通过：YES；入库不通过：NO
        public _InStockPurchaseBill()
        {
            InitializeComponent();
            bl = new InStockPurchaseBillBLL();
            lbPurchaseBill.Enabled = true;
            ControInittialState();
            DataBinding.SupplierComboBox(cbSupplier);//供应商下拉菜单
        }
        private void ControInittialState()
        {
            dtpPurchaseDate.Enabled = false;
            cbSupplier.Enabled = txtOnpress.Enabled = txtTotal.Enabled = txtPurchaseID.Enabled = txtStockDate.Enabled = false;
            btnCancel.Enabled = btnConfirm.Enabled = false;
            txtMemo.Enabled = true;
            gbSearch.Hide();
            dgvPurchaseDetail.Hide();

        }

        /// <summary>
        /// 获取待入库订单列表
        /// </summary>
        private List<string> GetInStockPurchaseBill()
        {
            IDList = bl.GetInStockPurchaseBill();
            return IDList;
        }
        /// <summary>
        /// 获取控件值
        /// </summary>
        private void GetValues()
        {
            bo = new PurchaseBill();
            bo.PurchaseBillID = txtPurchaseID.Text.Trim();
            bo.Custodian = "800001";
            bo.Memo = txtMemo.Text.Trim();
        }
        private void _InStockPurchaseBill1_Load(object sender, EventArgs e)
        {

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (opera == "YES")
                if (bl.AgreeInStock(bo))
                {
                    MessageBox.Show("订单审核通过并完成入库！", "系统提示：");
                    lbPurchaseBill.DataSource = GetInStockPurchaseBill();//更新ListBox控件数据源
                    ControInittialState();
                }
            if (opera == "NO")
                if (bl.NotAgreeInStock(bo))
                {
                    MessageBox.Show("订单不允许入库！", "系统提示：");
                    lbPurchaseBill.DataSource = GetInStockPurchaseBill();//更新ListBox控件数据源
                    ControInittialState();
                }
            btnInStock.Enabled = btnNoInStock.Enabled = true;
        }

        private void btnInStock_Click(object sender, EventArgs e)
        {
            btnInStock.Enabled = false;
            btnNoInStock.Enabled = false;
            btnCancel.Enabled = btnConfirm.Enabled = true ;


            GetValues();
            opera = "YES";
        }

        private void lbPurchaseBill_VisibleChanged(object sender, EventArgs e)
        {
            if (GetInStockPurchaseBill().Count == 0)
            {
                lbPurchaseBill.DataSource = null;
            }
            else
            {
                lbPurchaseBill.DataSource = GetInStockPurchaseBill();
                lbPurchaseBill.SelectedIndex = -1;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInStock.Enabled = btnNoInStock.Enabled = true;
            ControInittialState();
            //ClearControls.ClearControl(this);
            lbPurchaseBill.DataSource = GetInStockPurchaseBill();//更新ListBox控件数据源
        }

        private void btnNoInStock_Click(object sender, EventArgs e)
        {
            btnInStock.Enabled = false;
            btnNoInStock.Enabled = false;
            btnCancel.Enabled = btnConfirm.Enabled = true;
            GetValues();
            opera = "NO";
        }
    }
}
